package com.geline.easyexcel.config;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.XmlUtil;
import com.geline.easyexcel.domain.DynamicSqlBean;
import com.geline.easyexcel.domain.EasyExcelProperties;
import com.geline.easyexcel.interfaces.DefaultDropDownHandler;
import com.geline.easyexcel.interfaces.DropDownHandler;
import com.geline.easyexcel.interfaces.ExcelCrudConsumer;
import com.geline.easyexcel.manager.DropDownService;
import com.geline.easyexcel.manager.DynamicSqlManager;
import com.geline.easyexcel.manager.EasyExcelManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.io.File;
import java.util.List;

/**
 * 启动类
 * @author: mengxin
 * @date: 2023/9/23 9:56
 */
@EnableConfigurationProperties(value = {EasyExcelProperties.class})
@Slf4j
public class EasyExcelAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public EasyExcelManager initEasyExcelManager(EasyExcelProperties properties, DynamicSqlManager sqlManager, ExcelCrudConsumer consumer){
        return new EasyExcelManager(properties, sqlManager, consumer);
    }

    /**
     * 导出Excel下拉框，数据处理器，需要自定义实现
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public DropDownHandler initDropDownHandler(EasyExcelProperties properties, JdbcTemplate jdbcTemplate){
        return new DefaultDropDownHandler(properties, jdbcTemplate);
    }

    @Bean
    @ConditionalOnMissingBean
    public DropDownService initExcelDropDownService(DropDownHandler handler){
        return new DropDownService(handler);
    }

    @Bean
    @ConditionalOnMissingBean
    public DynamicSqlManager initDynamicSqlManager(EasyExcelProperties properties, JdbcTemplate jdbcTemplate){
        DynamicSqlManager manager = new DynamicSqlManager(jdbcTemplate);
        String path = properties.getXmlFile();
        String dirPath = path.substring(0, path.lastIndexOf("/"));
        List<File> fileList = FileUtil.loopFiles(dirPath);
        if(fileList!=null && fileList.size()>0){
            for(File file : fileList){
                //entity.xml
                String name = file.getName();
                if(name.toLowerCase().endsWith(".xml")){
                    String xmlText = FileUtil.readUtf8String(file);
                    Document document = XmlUtil.parseXml(xmlText);
                    Element root = XmlUtil.getRootElement(document);
                    DynamicSqlBean bean = XmlUtil.xmlToBean(root, DynamicSqlBean.class);
                    String entityName = name.substring(0, name.lastIndexOf("."));
                    manager.putDynamicSqlBean(StrUtil.lowerFirst(entityName), bean);
                    log.info("putDynamicSqlBean entityName:{}", entityName);
                }
            }
        }
        return manager;
    }
}
